package com.hua.yuan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.dialog.SortListPop;
import com.hua.yuan.ui.adapter.FuFJpAdapter;
import com.hua.yuan.ui.data.VideoDataRequest;
import com.hua.yuan.ui.module.MovieInfo69Bean;
import com.hua.yuan.ui.module.MovieInfoBean;
import com.hua.yuan.util.CheckLoginUtil;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/9.
 * 付费精品
 */

public class ActivityFuFJp extends BaseActivity {

    private ImageView backImage;
    private TextView titleView;

    private LinearLayout ll_sort;
    private TextView tv_sort;
    private ListView listView;
    private FuFJpAdapter adapter;

    private boolean flag = true;

    private int start = 0;

    private List<MovieInfo69Bean> cacheList = new ArrayList<>();
    private PopupWindow popupWindow;
    private int selectPos = 0;
    private List<MovieInfo69Bean> orlist = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_item);

        initView();
        initData();

    }

    private void initView() {

        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);
        ll_sort = findViewById(R.id.ll_sort);
        tv_sort = findViewById(R.id.tv_sort);
        listView = findViewById(R.id.listView);
        adapter = new FuFJpAdapter(this);

    }

    private void initData() {

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        titleView.setText("付费精品");

        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (CheckLoginUtil.check()) {

                    MovieInfoBean bean = adapter.getList().get(i);
                    startActivity(new Intent(ActivityFuFJp.this, VideoDisActivity.class).putExtra("id", bean.getId()));


                } else {
                    CheckLoginUtil.goToLogin(ActivityFuFJp.this);
                }

            }
        });

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int i) {
                if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    //判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {

                        getData();

                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        createData();

        ll_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow = SortListPop.getInstance().create(ActivityFuFJp.this);
                popupWindow.showAsDropDown(ll_sort);
                SortListPop.getInstance().initPosState(selectPos);
            }
        });
        SortListPop.setOnSelectClickListener(new SortListPop.OnSelectClickListener() {
            @Override
            public void onSelectClick(int position) {
                popupWindow.dismiss();
                selectPos = position;
                if (position == 0) {
                    List<MovieInfo69Bean> oldList = adapter.getList();
                    if (oldList == null) {
                        adapter.setList(orlist);
                    } else if (oldList.size() == 0) {
                        adapter.setList(orlist);
                    } else {
                        adapter.getList().clear();
                        oldList.addAll(orlist);
                        adapter.setList(oldList);
                    }

                    adapter.notifyDataSetChanged();
                    tv_sort.setText("按时间排序");

                } else if (position == 1) {
                    //升序
                    cacheList.clear();
                    cacheList.addAll(orlist);

                    Comparator cmp = new ChineseCharComp();
                    Collections.sort(cacheList, cmp);

                    adapter.getList().clear();
                    adapter.getList().addAll(cacheList);
                    adapter.notifyDataSetChanged();
                    tv_sort.setText("A-Z升序");

                } else if (position == 2) {
                    cacheList.clear();
                    cacheList.addAll(orlist);

                    Comparator cmp = new ChineseCharComp();
                    Collections.sort(cacheList, cmp);

                    adapter.getList().clear();
                    for (int i = cacheList.size() - 1; i >= 0; i--) {
                        adapter.getList().add(cacheList.get(i));
                    }
                    adapter.notifyDataSetChanged();
                    tv_sort.setText("A-Z降序");
                }
            }
        });
    }

    private void createData() {
        getData();
    }


    /**
     * 获取数据
     */
    private void getData() {

        if (flag) {

            flag = false;
            String token = PreferenceUtils.getString(Constents.TOKEN, "");


            VideoDataRequest.getMovies(token, 2, start, 10, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    flag = true;
                }

                @Override
                public void onResponse(String response, int id) {

                    flag = true;

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        if (!jsonObject.isNull("errno")) {
                            String code = jsonObject.getString("errno");

                            if ("0".equals(code)) {

                                if (!jsonObject.isNull("data")) {

                                    Gson gson = new Gson();

                                    List<MovieInfo69Bean> list = new ArrayList<>();

                                    JSONArray dataJson = jsonObject.getJSONArray("data");

                                    for (int i = 0; i < dataJson.length(); i++) {
                                        JSONObject data = dataJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(data.toString(), MovieInfo69Bean.class);
                                        list.add(bean);
                                        orlist.add(bean);
                                    }

                                    if (list.size() > 0) {
                                        start = start + 10;

                                        //刷行数据
                                        if (adapter != null) {
                                            List<MovieInfo69Bean> oldList = adapter.getList();

                                            if (oldList == null) {
                                                adapter.setList(list);
                                            } else if (oldList.size() == 0) {
                                                adapter.setList(list);
                                            } else {
                                                oldList.addAll(list);
                                                adapter.setList(oldList);
                                            }

                                            adapter.notifyDataSetChanged();
                                        }


                                    }


                                }


                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            });


        }


    }

    public class ChineseCharComp implements Comparator<MovieInfo69Bean> {
        public int compare(MovieInfo69Bean o1, MovieInfo69Bean o2) {
            Collator myCollator = Collator.getInstance(java.util.Locale.CHINA);
            if (myCollator.compare(o1.getTitle(), o2.getTitle()) < 0)
                return -1;
            else if (myCollator.compare(o1.getTitle(), o2.getTitle()) > 0)
                return 1;
            else
                return 0;
        }
    }
}
